﻿Imports System.Data.SqlClient

Public Class clsKmsDetalle
    Private mintIdDetalle As Integer
    Private mintIdAutobus As Integer
    Private mdatFecha As Date
    Private mdblKms As Double
    Private mdblKmsTrabajados As Double
    Private mdblKmsMuertos As Double
    Public Property IdDetalle() As Integer
        Get
            Return mintIdDetalle
        End Get
        Set(ByVal value As Integer)
            mintIdDetalle = value
        End Set
    End Property
    Public Property IdAutobus() As Integer
        Get
            Return mintIdAutobus
        End Get
        Set(ByVal value As Integer)
            mintIdAutobus = value
        End Set
    End Property
    Public Property Fecha() As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
    Public Property Kms() As Double
        Get
            Return mdblKms
        End Get
        Set(ByVal value As Double)
            mdblKms = value
        End Set
    End Property
    Public Property KmsTrabajados() As Double
        Get
            Return mdblKmsTrabajados
        End Get
        Set(ByVal value As Double)
            mdblKmsTrabajados = value
        End Set
    End Property
    Public Property KmsMuertos() As Double
        Get
            Return mdblKmsMuertos
        End Get
        Set(ByVal value As Double)
            mdblKmsMuertos = value
        End Set
    End Property
    Public Sub New()
        mintIdDetalle = 0
        mintIdAutobus = 0
        mdatFecha = Now()
        mdblKms = 0
        mdblKmsTrabajados = 0
        mdblKmsMuertos = 0
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into KmsDetalle (" & _
                            "IdAutobus," & _
                            "Fecha," & _
                            "Kms," & _
                            "KmsTrabajados," & _
                            "KmsMuertos)" & _
                            "VALUES (" & _
                            "@IdAutobus," & _
                            "@Fecha," & _
                            "@Kms," & _
                            "@KmsTrabajados," & _
                            "@KmsMuertos)" & _
                            " SELECT SCOPE_IDENTITY()"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("Kms", mdblKms)
        Command.Parameters.AddWithValue("KmsTrabajados", mdblKmsTrabajados)
        Command.Parameters.AddWithValue("KmsMuertos", mdblKmsMuertos)

        cnConn.Open()


        Try
            IdDetalle = Convert.ToInt32(Command.ExecuteScalar)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        End Try

        If IdDetalle > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el detalle diario ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub ActualizarFROMREGISTRO()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update KmsDetalle Set " & _
                    "Kms=@Kms, " & _
                    "KmsMuertos=@KmsMuertos " & _
                    "Where IdDetalle=@IdDetalle"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdDetalle", mintIdDetalle)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("Kms", mdblKms)
        Command.Parameters.AddWithValue("KmsTrabajados", mdblKmsTrabajados)
        Command.Parameters.AddWithValue("KmsMuertos", mdblKmsMuertos)
        cnConn.Open()

        Dim RowsAffected As Integer

        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el liquidación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
    Public Sub ActualizarFROMHISTORICO()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update KmsDetalle Set " & _
                    "KmsTrabajados=@KmsTrabajados, " & _
                    "KmsMuertos=@KmsMuertos " & _
                    "Where IdDetalle=@IdDetalle"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdDetalle", mintIdDetalle)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("Kms", mdblKms)
        Command.Parameters.AddWithValue("KmsTrabajados", mdblKmsTrabajados)
        Command.Parameters.AddWithValue("KmsMuertos", mdblKmsMuertos)
        cnConn.Open()

        Dim RowsAffected As Integer

        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el liquidación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
